﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Xml;
using System.Windows.Markup;
using S7.UserControls;
using System.Threading;
using M7;

namespace S7
{
	/// <summary>
	/// Interaction logic for Table.xaml
	/// </summary>
	public partial class TableControl : UserControl
	{
        public TableControl()
		{
			this.InitializeComponent();
            TableNumber = "0";
		}

        public string TableNumber
        {
            get { return tbTableNumber.Text; }
            set { tbTableNumber.Text = value; }
        }
        public string Info1
        {
            get { return tbInfo1.Text; }
            set { tbInfo1.Text = value; }
        }
        public string Info2
        {
            get { return tbInfo2.Text; }
            set { tbInfo2.Text = value; }
        }
        public string Info3
        {
            get { return tbInfo3.Text; }
            set { tbInfo3.Text = value; }
        }
        public string Info4
        {
            get { return tbInfo4.Text; }
            set { tbInfo4.Text = value; }
        }

        private bool _IsConnected = false;

        public bool IsConnected
        {
            get { return _IsConnected; }
            set { _IsConnected = value; }
        }

        private ClientConnection _connection;

        public ClientConnection Connection
        {
            get { return _connection; }
            set { _connection = value; }
        }

        private void SignCustomersOut_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            if (IsConnected)
            {
                MessageBox.Show("Can't delete a connected client", "Table", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
            }
            else
            {
                int id = int.Parse(TableNumber);
                Thread thr = new Thread(() =>
                    {
                        try
                        {
                            Table.lstTables = Table.Search();
                            var tables = from table in Table.lstTables
                                         where table.TableID.Equals(id)
                                         select table;
                            foreach (var table in tables)
                            {
                                table.Delete();
                            }
                            Info.window.wrpLiveView.Dispatcher.Invoke(new ThreadStart(() =>
                            {
                                try
                                {
                                    Info.window.wrpLiveView.Children.Remove(this);
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                                }
                            }));
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                        }
                    });
                thr.IsBackground = true;
                thr.Start();
            }
        }

        private void ShowAllOrders_Click(object sender, RoutedEventArgs e)
        {
            //edy
        }

        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            //int id = int.Parse(TableNumber);
            //if (IsConnected)
            //{
            //    var cons = from cont in Info.server.con.Values.OfType<ClientConnection>().ToList()
            //               where cont.current.TableID.Equals(id)
            //               select cont;
            //    foreach (var item in cons)
            //    {
            //        Msg SendMsg = new Msg();
            //        SendMsg.IP = item.current.IP;
            //        SendMsg.port = item.current.Port;
            //        SendMsg.enumMethod = enumAdminTools.GetStatus;
            //        Info.server.SendingQueue.Enqueue(SendMsg);
            //    }
            //}
            //else
            //{
            //    var cons = from cont in Info.server.con.Values.OfType<ClientConnection>().ToList()
            //               where cont.current.TableID.Equals(id)
            //               select cont;
            //    foreach (var item in cons)
            //    {
            //        Msg SendMsg = new Msg();
            //        SendMsg.IP = item.current.IP;
            //        SendMsg.port = item.current.Port;
            //        SendMsg.enumMethod = enumConnection.ConnectionConnect;
            //        Info.server.SendingQueue.Enqueue(SendMsg);
            //    }
            //}
        }

        public void AddChair(string UserName, int ID)
        {
            var c = new Chair();
            c.CustomerName = UserName;
            c.CustomerID = ID;
            rpChairs.Children.Add(c);
            c.ChangeColor(Chair.Waiting.Online);
        }

        public void RemoveChair(int ID)
        {
            try
            {
                foreach (Chair ch in rpChairs.Children)
                {
                    if (ch.CustomerID == ID)
                        rpChairs.Children.Remove(ch);
                    break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        public void ChangeColor(Chair.Waiting w)
        {
            switch (w)
            {
                case Chair.Waiting.Waiter:
                    eTable.Fill = (Brush)Application.Current.Resources["bWaiter"];
                    break;
                case Chair.Waiting.Order:
                    eTable.Fill = (Brush)Application.Current.Resources["bOrder"];
                    break;
                case Chair.Waiting.WC:
                    eTable.Fill = (Brush)Application.Current.Resources["bWC"];
                    break;
                case Chair.Waiting.Cab:
                    eTable.Fill = (Brush)Application.Current.Resources["bCab"];
                    break;
                case Chair.Waiting.Online:
                    eTable.Fill = (Brush)Application.Current.Resources["bOnline"];
                    break;
                case Chair.Waiting.Offline:
                    eTable.Fill = (Brush)Application.Current.Resources["bOffline"];
                    break;
            }
	}
}}